<template>
  <div>
    <!-- //大div -->
    <div class="top_nav">
      <input
        type="text"
        @touchstart="top_nav_input"
        
      
        placeholder="回车搜索商品"
        class="input_top"
      />
      <br />
      <div class="top_right">
        <div v-for="(item, index) in category_list" :key="index">
          <br />
          <p
            @click="onclick(index)"
            :class="topindex===index?'nametitle':''"
            style="text-align:center"
          >{{item.name}}</p>
        </div>
      </div>
      <div class="top_left">
        <div class="block">
          <br />
          <div style="width:80%;" class="top_slide">
            <el-carousel height="150px">
              <el-carousel-item v-for="(item, index) in imglist" :key="index">
                <h3 class="small">
                  <img width="100%" :src="item.img" style="  border-radius: 20px" alt />
                </h3>
              </el-carousel-item>
            </el-carousel>
          </div>
        </div>
        <div style="float:left;width:100%;height:86.9vh">
          <div
            @click="onclicklist(item.pid)"
            v-for="(item, index) in category_item"
            :key="index"
            style="width:30%;float:left  ;height:11vh;font-size:12px; margin-left:3%;margin-top:13%"
          >
            <img width="100%" height="100%" :src="item.icon" alt />
            <p style="text-align:center">{{item.name}}</p>
          </div>
        </div>
      </div>
    </div>
  </div>
</template>

<script>
export default {
  data() {
    return {
      category: [],
      category_item: [],
      category_list: [],
      topindex: 0,
      imglist: [
        {
          img: require("../../public/1.jpg"),
        },
        {
          img: require("../../public/2.jpg"),
        },
        {
          img: require("../../public/3.jpg"),
        },
      ],
    };
  },
  mounted() {
    this.$axios.category({}).then((res) => {
      // console.log(res.data.data);
      this.category = res.data.data;
      this.category_list.push({
        name: "所有分类",
        pid: 0,
      });
      this.category.forEach((ele) => {
        if (ele.pid === 0) {
          this.category_list.push(ele);
        }
        if (ele.pid !== 0) {
          this.category_item.push(ele);
        }
      });
    });
  },

  methods: {
    top_nav_input() {
      this.$router.push("input_button");
    },
    onclicklist(id) {
      this.$router.push({
        path: "/categorylist",
        query: {
          id,
        },
      });
    },
    onclick(index) {
      this.topindex = index;
      this.category_item = [];
      if (this.topindex === 0) {
        this.category.forEach((ele) => {
          this.category_item.push(ele);
        });
      } else {
        this.category.forEach((ele) => {
          if (ele.pid === this.category_list[this.topindex].id) {
            this.category_item.push(ele);
          }
        });
      }
    },
  },
};
</script>

<style scoped>
.top_slide {
  margin-left: 11%;
  border-radius: 20px;
}
.el-carousel__item h3 {
  color: #475669;
  font-size: 14px;
  opacity: 0.75;
  line-height: 150px;
  border-radius: 20px;
  margin: 0;
}

.el-carousel__item:nth-child(2n) {
  background-color: #99a9bf;
  border-radius: 20px;
}

.el-carousel__item:nth-child(2n + 1) {
  background-color: #d3dce6;
  border-radius: 20px;
}
.small {
  border-radius: 20px;
}
.input_top {
  height: 3.7vh;
  width: 70%;
  font-size: 12px;
  margin-left: 3%;
  border-radius: 20px;
  background: rgb(233, 233, 233);
  margin-top: 1vh;
  padding-left: 10px;
}
.top_nav {
  width: 100%;
  height: 86.9vh;

  float: left;
}
.top_right {
  width: 20%;
  overflow: scroll;
  height: 86.9vh;

  float: left;
}
.top_left {
  overflow: scroll;
  width: 80%;
  height: 86.9vh;

  float: left;
}
.nametitle {
  color: red;
  border-left: 2px solid red;
}
</style>